package com.black.apartment.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.black.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.black.apartment.domain.ApartmentAttributes;
import com.black.apartment.mapper.ApartmentsMapper;
import com.black.apartment.domain.Apartments;
import com.black.apartment.service.IApartmentsService;

/**
 * 公寓管理Service业务层处理
 * 
 * @author kuan
 * @date 2024-10-08
 */
@Service
public class ApartmentsServiceImpl implements IApartmentsService 
{
    @Autowired
    private ApartmentsMapper apartmentsMapper;

    /**
     * 查询公寓管理
     * 
     * @param id 公寓管理主键
     * @return 公寓管理
     */
    @Override
    public Apartments selectApartmentsById(Long id)
    {
        return apartmentsMapper.selectApartmentsById(id);
    }

    /**
     * 查询公寓管理列表
     * 
     * @param apartments 公寓管理
     * @return 公寓管理
     */
    @Override
    public List<Apartments> selectApartmentsList(Apartments apartments)
    {
        return apartmentsMapper.selectApartmentsList(apartments);
    }

    /**
     * 新增公寓管理
     * 
     * @param apartments 公寓管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertApartments(Apartments apartments)
    {
        int rows = apartmentsMapper.insertApartments(apartments);
        insertApartmentAttributes(apartments);
        return rows;
    }

    /**
     * 修改公寓管理
     * 
     * @param apartments 公寓管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateApartments(Apartments apartments)
    {
        apartmentsMapper.deleteApartmentAttributesByApartmentId(apartments.getId());
        insertApartmentAttributes(apartments);
        return apartmentsMapper.updateApartments(apartments);
    }

    /**
     * 批量删除公寓管理
     * 
     * @param ids 需要删除的公寓管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApartmentsByIds(Long[] ids)
    {
        apartmentsMapper.deleteApartmentAttributesByApartmentIds(ids);
        return apartmentsMapper.deleteApartmentsByIds(ids);
    }

    /**
     * 删除公寓管理信息
     * 
     * @param id 公寓管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApartmentsById(Long id)
    {
        apartmentsMapper.deleteApartmentAttributesByApartmentId(id);
        return apartmentsMapper.deleteApartmentsById(id);
    }

    /**
     * 新增公寓属性信息信息
     * 
     * @param apartments 公寓管理对象
     */
    public void insertApartmentAttributes(Apartments apartments)
    {
        List<ApartmentAttributes> apartmentAttributesList = apartments.getApartmentAttributesList();
        Long id = apartments.getId();
        if (StringUtils.isNotNull(apartmentAttributesList))
        {
            List<ApartmentAttributes> list = new ArrayList<ApartmentAttributes>();
            for (ApartmentAttributes apartmentAttributes : apartmentAttributesList)
            {
                apartmentAttributes.setApartmentId(id);
                list.add(apartmentAttributes);
            }
            if (list.size() > 0)
            {
                apartmentsMapper.batchApartmentAttributes(list);
            }
        }
    }
}
